﻿Imports IControl.IControls

Public Class IFormDonBK
    Private iclass As IClass = New IClass
    Private ds As DataSet
    Private dt As DataTable
    Private bd As BindingSource
    Private am, ad As Object
    Private state As Integer
    Protected controlFocus As Control
    Public Overridable Sub setSubTruoc_Luu()
    End Sub
    Public Overridable Sub setSubSau_Luu()
    End Sub
    Public Overridable Sub setSubTruoc_TaoMoi()
    End Sub
    Public Overridable Sub setSubSau_TaoMoi()
    End Sub
    Public Overridable Sub setSubTruoc_Sua()
    End Sub
    Public Overridable Sub setSubSau_Sua()
    End Sub
    Public Overridable Sub setSubTruoc_Xoa()
    End Sub
    Public Overridable Sub setSubSau_Xoa()
    End Sub
    Public Overridable Sub setSubTruoc_HuyThayDoi()
    End Sub
    Public Overridable Sub setSubSau_HuyThayDoi()
    End Sub
    Public Overridable Sub setSubTruoc_In()
    End Sub
    Public Overridable Sub setSubSau_In()
    End Sub
    Public Overridable Sub setSubTruoc_Thoat()
    End Sub
    Public Overridable Sub setSubTruoc_LamTuoi()
    End Sub
    Public Overridable Sub setSubSau_LamTuoi()
    End Sub
    Public Overridable Sub setSubTruoc_DauTien()
    End Sub
    Public Overridable Sub setSubSau_DauTien()
    End Sub
    Public Overridable Sub setSubTruoc_Truoc()
    End Sub
    Public Overridable Sub setSubSau_Truoc()
    End Sub
    Public Overridable Sub setSubTruoc_Sau()
    End Sub
    Public Overridable Sub setSubSau_Sau()
    End Sub
    Public Overridable Sub setSubTruoc_Cuoi()
    End Sub
    Public Overridable Sub setSubSau_Cuoi()
    End Sub
    '=====================================================
    Public Overridable Function setFuncTruoc_Luu() As Boolean
        Return True
    End Function
    Public Overridable Function setFuncSau_Luu() As Boolean
        Return True
    End Function
    Public Overridable Function setFuncTruoc_TaoMoi() As Boolean
        Return True
    End Function
    Public Overridable Function setFuncSau_TaoMoi() As Boolean
        Return True
    End Function
    Public Overridable Function setFuncTruoc_Sua() As Boolean
        Return True
    End Function
    Public Overridable Function setFuncSau_Sua() As Boolean
        Return True
    End Function
    Public Overridable Function setFuncTruoc_HuyThayDoi() As Boolean
        Return True
    End Function
    Public Overridable Function setFuncTruoc_Xoa() As Boolean
        Return True
    End Function
    Public Overridable Function setFuncSau_Xoa() As Boolean
        Return True
    End Function
    Public Overridable Function setFuncSau_HuyThayDoi() As Boolean
        Return True
    End Function
    Public Overridable Function setFuncTruoc_In() As Boolean
        Return True
    End Function
    Public Overridable Function setFuncSau_in() As Boolean
        Return True
    End Function
    Public Overridable Function setFuncTruoc_Thoat() As Boolean
        Return True
    End Function
    Public Overridable Function setFuncTruoc_LamTuoi() As Boolean
        Return True
    End Function
    Public Overridable Function setFuncSau_LamTuoi() As Boolean
        Return True
    End Function
    Public Overridable Function setFuncTruoc_DauTien() As Boolean
        Return True
    End Function
    Public Overridable Function setFuncSau_DauTien() As Boolean
        Return True
    End Function
    Public Overridable Function setFuncTruoc_Truoc() As Boolean
        Return True
    End Function
    Public Overridable Function setFuncSau_Truoc() As Boolean
        Return True
    End Function
    Public Overridable Function setFuncTruoc_Sau() As Boolean
        Return True
    End Function
    Public Overridable Function setFuncSau_Sau() As Boolean
        Return True
    End Function
    Public Overridable Function setFuncTruoc_Cuoi() As Boolean
        Return True
    End Function
    Public Overridable Function setFuncSau_Cuoi() As Boolean
        Return True
    End Function
    '========= end các function khi xử lý ============
    ''' <summary>
    ''' set các đối tượng của form đơn
    ''' </summary>
    ''' <param name="ds">Tên DataSet</param>
    ''' <param name="dt">Tên Datatable</param>
    ''' <param name="bd">Tên BidingSource</param>
    ''' <param name="am">Tên TableAdapterManager</param>
    ''' <param name="ad">Tên TableAdapter</param>
    ''' <param name="igridview">Tên IGridView</param>
    ''' <param name="controlfocus">Tên control bắt focus khi tạo mới</param>
    ''' <remarks></remarks>
    Public Sub setObj(ByVal ds As DataSet, ByVal dt As DataTable, ByVal bd As BindingSource, ByVal am As Object, ByVal ad As Object, ByVal igridview As IGridView, ByVal controlfocus As Control)
        Me.bd = bd
        Me.ds = ds
        Me.dt = dt
        Me.am = am
        Me.ad = ad
        Me.IGridView1 = igridview
        Me.controlFocus = controlfocus
        iclass.setObj(ds, dt, bd, am, ad, igridview, controlfocus)
    End Sub
    Private Function getKiemTraMaTonTai(ByVal sMa As String) As Boolean 'kiểm tra sự tồn tại của mã - dùng kiểm tra chung với igridview
        If Me.bd.Find("ma", sMa) = -1 Then 'nếu -1 nghĩa là ko tìm đc vị trí trong bindingsource
            Return False
        Else 'còn lại là tìm đc vị trí --> đã tồn tại
            Return True
        End If
    End Function
    Private Sub setReadonlyControl(ByVal b As Boolean)
        setReadonly(Me.LayoutControl1, b)
        If b Then
            Try
                Me.IGridView1.OptionsBehavior.Editable = False
                Me.IGridView1.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None
            Catch ex As Exception
            End Try
        Else
            Try
                Me.IGridView1.OptionsBehavior.Editable = True
                Me.IGridView1.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom
            Catch ex As Exception
            End Try
        End If
        If b Then
            Me.DxValidationProvider1.ValidationMode = DevExpress.XtraEditors.DXErrorProvider.ValidationMode.Manual
            setButtonTrangThaiSua(False)
        Else
            Me.DxValidationProvider1.ValidationMode = DevExpress.XtraEditors.DXErrorProvider.ValidationMode.Auto
            setButtonTrangThaiSua(True)
        End If
    End Sub
    Private Sub setButtonTrangThaiSua(ByVal b As Boolean) 'set nút lưu và xóa - khi ở trạng thái sửa thì mới cho phép thao tác trên nó (enable = true) - các trạng thái khác thì không cho (enable = false)
        Me.BarButtonItemLuu.Enabled = b
        Me.BarButtonItemXoa.Enabled = b
        If b Then 'xử lý nút sửa (nếu nút lưu - xóa cho phép thì sửa ko cho phép và ngược lại)
            Me.BarButtonItemTaoMoi.Enabled = False
            Me.BarButtonItemSua.Enabled = False
        Else
            Me.BarButtonItemTaoMoi.Enabled = True
            Me.BarButtonItemSua.Enabled = True
        End If
    End Sub
    Private Sub BarButtonItemTaoMoi_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemTaoMoi.ItemClick
        Me.setSubTruoc_TaoMoi()
        If setFuncTruoc_TaoMoi() Then
            Me.setReadonlyControl(False)
            If Me.BarCheckItemNhapTuDanhSach.Checked Then
                Me.IGridView1.setFocusDongDauTien()
                Me.LayoutControlGroupThongTin.Expanded = False
            Else
                Me.LayoutControlGroupThongTin.Expanded = True
                Me.controlFocus.Focus()
                iclass.getNew()
            End If
            Me.state = 1
            Me.setSubSau_TaoMoi()
        End If
    End Sub
    Private Sub BarButtonItemLuu_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemLuu.ItemClick
        Me.setSubTruoc_Luu()
        If Me.setFuncTruoc_Luu() Then
            If state = 1 And Me.BarCheckItemNhapTuDanhSach.Checked = False Then
                If getKiemTraMaTonTai(controlFocus.Text) Then
                    DevExpress.XtraEditors.XtraMessageBox.Show("'" & controlFocus.Text & "' đã tồn tại !", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    Me.controlFocus.Focus()
                    Exit Sub
                End If
            End If
            'IGridView1.setUpdateData()
            If iclass.getSave() And state <> 0 Then
                setReadonlyControl(True)
                Me.state = 0
                Me.setSubSau_Luu()
            End If
        End If
    End Sub
    Private Sub BarButtonItemSua_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemSua.ItemClick
        Me.setSubTruoc_Sua()
        If Me.setFuncTruoc_Sua Then
            setReadonlyControl(False)
            state = 2
            Me.setSubSau_Sua()
        End If
    End Sub
    Private Sub BarButtonItemXoa_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemXoa.ItemClick
        Me.setSubTruoc_Xoa()
        If Me.setFuncTruoc_Xoa Then
            If Me.iclass.getDelete() Then
                setReadonlyControl(True)
                Me.state = 0
                Me.setSubSau_Xoa()
            End If
        End If
    End Sub
    Private Sub BarButtonItemLamTuoi_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemLamTuoi.ItemClick
        Me.setSubTruoc_LamTuoi()
        If Me.setFuncTruoc_LamTuoi Then
            If Me.iclass.getRefresh() Then
                setReadonlyControl(True)
                Me.state = 0
                Me.setSubSau_LamTuoi()
            End If
        End If
    End Sub
    Private Sub BarButtonItemBaoCao_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemBaoCao.ItemClick
        Me.setSubTruoc_In()
        If Me.setFuncTruoc_In() Then
            Me.setSubSau_In()
        End If
    End Sub
    Private Sub BarButtonItemHuyThayDoi_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemHuyThayDoi.ItemClick
        Me.setSubTruoc_HuyThayDoi()
        If Me.setFuncTruoc_HuyThayDoi Then
            If Me.iclass.getUndo() Then
                setReadonlyControl(True)
                Me.state = 0
                Me.setSubSau_HuyThayDoi()
            End If
        End If
    End Sub
    Private Sub BarButtonItemFirst_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemFirst.ItemClick
        Me.setSubTruoc_DauTien()
        If Me.setFuncTruoc_DauTien Then
            Me.iclass.getMoveFirst()
            Me.setSubSau_DauTien()
        End If
    End Sub
    Private Sub BarButtonItemPrevious_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemPrevious.ItemClick
        Me.setSubTruoc_Truoc()
        If Me.setFuncTruoc_Truoc Then
            Me.iclass.getMovePrevious()
            Me.setSubSau_Truoc()
        End If
    End Sub
    Private Sub BarButtonItemNext_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemNext.ItemClick
        Me.setSubTruoc_Sau()
        If Me.setFuncTruoc_Sau() Then
            Me.iclass.getMoveNext()
            Me.setSubSau_Sau()
        End If
    End Sub
    Private Sub BarButtonItemLast_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemLast.ItemClick
        Me.setSubTruoc_Cuoi()
        If Me.setFuncTruoc_Cuoi Then
            Me.iclass.getMoveLast()
            Me.setSubSau_Cuoi()
        End If
    End Sub
    Private Sub BarButtonItemThoat_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemThoat.ItemClick
        Me.setSubTruoc_Thoat()
        If Me.setFuncTruoc_Thoat Then
            If Me.iclass.getSave() Then
                Me.Dispose()
                Me.Close()
            End If
        End If
    End Sub

    Private Sub IFormDon_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Me.setSubTruoc_Thoat()
        If Me.setFuncTruoc_Thoat Then
            If Me.iclass.getSave() Then
                Me.Dispose()
                Me.Close()
            End If
        End If
    End Sub
    Private Sub FormDon1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        setReadonlyControl(True)
    End Sub
    Private Sub IGridView1_InvalidValueException(sender As Object, e As DevExpress.XtraEditors.Controls.InvalidValueExceptionEventArgs) Handles IGridView1.InvalidValueException
        e.ExceptionMode = DevExpress.XtraEditors.Controls.ExceptionMode.DisplayError
    End Sub

    Private Sub IGridView1_ValidatingEditor(sender As Object, e As DevExpress.XtraEditors.Controls.BaseContainerValidateEditorEventArgs) Handles IGridView1.ValidatingEditor
        If Me.IGridView1.FocusedColumn.FieldName = "ma" And IGridView1.FocusedRowHandle <> -999997 Then
            If Me.getKiemTraMaTonTai(e.Value) And state = 1 Then
                e.ErrorText = "Mã '" & e.Value & "'đã tồn tại !"
                e.Valid = False
            End If
        End If
    End Sub
End Class